using System;
using System.Collections;

namespace OracleInternal.Network
{
	internal sealed class NVTokens
	{
		internal const int TKN_NONE = 0;

		internal const int TKN_LPAREN = 1;

		internal const int TKN_RPAREN = 2;

		internal const int TKN_COMMA = 3;

		internal const int TKN_EQUAL = 4;

		internal const int TKN_LITERAL = 8;

		internal const int TKN_EOS = 9;

		private const char TKN_LPAREN_VALUE = '(';

		private const char TKN_RPAREN_VALUE = ')';

		private const char TKN_COMMA_VALUE = ',';

		private const char TKN_EQUAL_VALUE = '=';

		private const char TKN_BKSLASH_VALUE = '\\';

		private const char TKN_DQUOTE_VALUE = '"';

		private const char TKN_SQUOTE_VALUE = '\'';

		private const char TKN_EOS_VALUE = '%';

		private const char TKN_SPC_VALUE = ' ';

		private const char TKN_TAB_VALUE = '\t';

		private const char TKN_LF_VALUE = '\n';

		private const char TKN_CR_VALUE = '\r';

		private ArrayList m_tkType;

		private ArrayList m_tkValue;

		private int m_numTokens;

		private int m_tkPos;

		internal int Token
		{
			get
			{
				if (m_tkType == null)
				{
					throw new NetworkException(303);
				}
				if (m_tkPos < m_numTokens)
				{
					return (int)m_tkType[m_tkPos];
				}
				throw new NetworkException(351);
			}
		}

		internal string Literal
		{
			get
			{
				string text = null;
				if (m_tkValue == null)
				{
					throw new NetworkException(303);
				}
				if (m_tkPos < m_numTokens)
				{
					return (string)m_tkValue[m_tkPos];
				}
				throw new NetworkException(351);
			}
		}

		internal NVTokens()
		{
			m_tkType = null;
			m_tkValue = null;
			m_numTokens = 0;
			m_tkPos = 0;
		}

		private static bool IsWhiteSpace(char it)
		{
			if (it == ' ' || it == '\t' || it == '\n' || it == '\r')
			{
				return true;
			}
			return false;
		}

		private static string TrimWhiteSpace(string it)
		{
			int length = it.Length;
			int i = 0;
			int num = length;
			for (; i < length && IsWhiteSpace(it[i]); i++)
			{
			}
			while (i < num && IsWhiteSpace(it[num - 1]))
			{
				num--;
			}
			return it.Substring(i, num - i);
		}

		internal bool ParseTokens(string nvString)
		{
			m_numTokens = 0;
			m_tkPos = 0;
			m_tkType = ArrayList.Synchronized(new ArrayList(25));
			m_tkValue = ArrayList.Synchronized(new ArrayList(25));
			int length = nvString.Length;
			int i = 0;
			while (i < length)
			{
				for (; i < length && IsWhiteSpace(nvString[i]); i++)
				{
				}
				if (i >= length)
				{
					continue;
				}
				switch (nvString[i])
				{
				case '(':
					AddToken(1, '(');
					i++;
					continue;
				case '=':
					AddToken(4, '=');
					i++;
					continue;
				case ')':
					AddToken(2, ')');
					i++;
					continue;
				case ',':
					AddToken(3, ',');
					i++;
					continue;
				}
				int num = i;
				int num2 = -1;
				bool flag = false;
				char c = '"';
				if (nvString[i] == '\'' || nvString[i] == '"')
				{
					flag = true;
					c = nvString[i];
					i++;
					num = i;
				}
				while (i < length)
				{
					if (nvString[i] == '\\' && i + 1 < length && nvString[i + 1] != ')')
					{
						i += 2;
						continue;
					}
					if (flag)
					{
						if (nvString[i] == c)
						{
							num2 = i;
							i++;
							break;
						}
					}
					else
					{
						if (nvString[i] == '\'' || nvString[i] == '"')
						{
							throw new NetworkException(303);
						}
						if (nvString[i] == '(' || nvString[i] == ')' || nvString[i] == ',' || nvString[i] == '=')
						{
							num2 = i;
							break;
						}
					}
					i++;
				}
				if (num2 == -1)
				{
					num2 = i;
				}
				AddToken(8, TrimWhiteSpace(nvString.Substring(num, num2 - num)));
			}
			AddToken(9, '%');
			return true;
		}

		internal string PopLiteral()
		{
			string text = null;
			if (m_tkValue == null)
			{
				throw new NetworkException(303);
			}
			if (m_tkPos < m_numTokens)
			{
				return (string)m_tkValue[m_tkPos++];
			}
			throw new NetworkException(351);
		}

		internal void EatToken()
		{
			if (m_tkPos < m_numTokens)
			{
				m_tkPos++;
			}
		}

		private void AddToken(int tk, char tk_char)
		{
			AddToken(tk, Convert.ToString(tk_char));
		}

		private void AddToken(int tk, string tk_val)
		{
			m_tkType.Add(tk);
			m_tkValue.Add(tk_val);
			m_numTokens++;
		}
	}
}
